﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class Form2

    Dim objClsSysParam As New clsSysParam
    Dim agv_website As String
    Dim objagvlog As New clsAgvLog
    Dim httphelper As New HttpHelper
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        connectDB()
        initAgvWebSite()
        initAgv()
    End Sub

    ''' <summary>
    ''' 连接数据库
    ''' </summary>
    Private Sub connectDB()

        g_szInitFileName = My.Application.Info.DirectoryPath & "\tmj_agv.ini"

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If

    End Sub
    Private Sub initAgvWebSite()
        Dim dt As DataTable = objClsSysParam.selectAgvWebSite()
        If dt.Rows.Count = 1 Then
            agv_website = dt.Rows(0)("param_value").ToString()
        End If
    End Sub

    Private Sub initAgv()
        Dim dt As DataTable = objClsSysParam.selectAgvTmj()
        cmbAgv.Text = dt.Rows(0)("param_value").ToString()
    End Sub

    Private Function setTask(agv As String, oper As Integer) As String
        Dim msg As String
        If String.IsNullOrEmpty(agv_website) = False Then
            Dim url As String = agv_website & "AGVAPI?do=SetAGVState&agv=" & agv & "&oper=" & oper
            Dim rtn As String = httphelper.HttpGet(url, "")
            Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
            Dim code As String = jo("code").ToString() '输出0
            msg = jo("msg").ToString() '输出ok
        Else
            msg = "请联系工作人员，配置agv网址"
        End If
        Return msg
    End Function

    Private Sub btnConmit_Click(sender As Object, e As EventArgs) Handles btnConmit.Click
        If String.IsNullOrEmpty(cmbAgv.Text) = False Then
            If MsgBox("请确保当前启动的AGV是需要启动的，要启动该AGV吗？", vbYesNo, "提示") = vbYes Then
                If cmbAgv.Text.Equals("0") = False Then
                    Dim localip As String = GetIPAddr()
                    If RadioButton1.Checked Then
                        Dim rtn As String = setTask(cmbAgv.Text, 1)
                        TextBox1.Text = rtn
                        objagvlog.add(localip, cmbAgv.Text, "启动", "人工启动")
                    End If
                    If RadioButton2.Checked Then
                        Dim rtn As String = setTask(cmbAgv.Text, 0)
                        TextBox1.Text = rtn
                        objagvlog.add(localip, cmbAgv.Text, "停止", "人工停止")
                    End If
                    initAgv()
                Else
                    MsgBox("当前没有可以启动的AGV")
                End If
            End If
        Else
            MsgBox("请选择AGV")
        End If

    End Sub


End Class